package com.droid.customcalendar.cal.view.adapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.droid.customcalendar.cal.model.MonthCalendarDataGenerator;
import com.droid.customcalendar.cal.view.MonthView;
import com.droid.customcalendar.cal.view.MonthView.OnDateSelectedListener;

public class MonthPagerAdapter extends PagerAdapter {

	private static final String MONTH_VIEW_TAG = "PagerAdapter.MONTH_VIEW_TAG_";
	
	private MonthCalendarDataGenerator[] monthCalendarDataGenerators;
	private OnDateSelectedListener dateSelectedListener;
	private List<IntervalData> backgroundResIntervals;
	private List<IntervalData> textColorResIntervals;

	public MonthPagerAdapter(MonthCalendarDataGenerator[] generators) {
		this.monthCalendarDataGenerators = generators;
		initIntervals();
	}

	private void initIntervals() {
		backgroundResIntervals = new ArrayList<IntervalData>();
		textColorResIntervals = new ArrayList<IntervalData>();
	}
	
	public void setOnDateSelectedListener(OnDateSelectedListener dateSelectedListener) {
		this.dateSelectedListener = dateSelectedListener;
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return monthCalendarDataGenerators.length;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void destroyItem(ViewGroup collection, int position, Object view) {
		ViewPager pager = (ViewPager) collection;
		pager.removeView((View) view);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		MonthCalendarDataGenerator calendarDataGenerator = monthCalendarDataGenerators[position];
		MonthView monthView = new MonthView(container.getContext());
		monthView.setOnDateSelectedListener(dateSelectedListener);
		monthView.setMonthDataGenerator(calendarDataGenerator);
		updateMonthUI(monthView);
		monthView.setTag(buildTag(position));
		container.addView(monthView);
		return monthView;
	}

	private void updateMonthUI(MonthView monthView) {
		for (IntervalData data: backgroundResIntervals) {
			monthView.setIntervalBackgroundResource(data.startDate, data.endDate, data.res);
		}
		for (IntervalData data: textColorResIntervals) {
			monthView.setIntervalTextColor(data.startDate, data.endDate, data.res);
		}
	}

	public String buildTag(int index) {
		return MONTH_VIEW_TAG + String.valueOf(index);
	}
	
	public void setIntervalBackgroundResId(Date startDate, Date endDate, int resId) {
		IntervalData data = new IntervalData();
		data.startDate = startDate;
		data.endDate = endDate;
		data.res = resId;
		backgroundResIntervals.add(data);
		notifyDataSetChanged();
	}

	public void setIntervalTextColor(Date startDate, Date endDate, int textColor) {
		IntervalData data = new IntervalData();
		data.startDate = startDate;
		data.endDate = endDate;
		data.res = textColor;
		textColorResIntervals.add(data);
		notifyDataSetChanged();
	}
	
	@Override
	//TODO - try to improve this
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	
	private static class IntervalData {
		public Date startDate;
		public Date endDate;
		public int res;
	}
}
